package com.fpx.order.plat.bussiness.controller;

import java.io.IOException;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fpx.api.bean.request.GetPaypalDetailsRequest;
import com.fpx.api.bean.response.GetTransDetailsResponse;
import com.fpx.api.paypal.bean.GetPaypalTransactionDetailsResponse;
import com.fpx.api.service.IPaypalService;
import com.fpx.api.util.Constant;
import com.fpx.order.plat.bussiness.cache.CacheManage;
import com.fpx.order.plat.bussiness.system.CacheConstants;


/**
 * paypal服务调用接口
 * desc: PaypalInterfaceController
 * @author xiangtao(xiangtao@4px.com)
 * @date 2016年1月6日 上午9:48:16
 * @version 1.0
 */
@Controller
public class PaypalInterfaceController {
	
	
	@Autowired
	private IPaypalService paypalService;
	
	
	@Autowired 
	private CacheManage cacheManage;
	
	/**
	 * 
	 * <p>Title: requestEbayDetails</p>
	 * <p>Desc: 获取paypal交易基础信息</p>
	 * @author xiangtao(xiangtao@4px.com)
	 * @date 2016年1月6日 上午9:49:22 
	 * @version 1.0
	 * @param httpRequest
	 * @return
	 * @throws IOException
	 */
	@ResponseBody
	@RequestMapping(value = "/requestPaypalTransDetails")
	public String requestEbayDetails(HttpServletRequest httpRequest) throws IOException{
		StringBuffer jsonBuffer = new StringBuffer("");
		try{
			String transactionId = httpRequest.getParameter("transactionId");
			//request参数封装
			GetPaypalDetailsRequest request = new GetPaypalDetailsRequest();
			//平台类型   paypal
			request.setPlatform(Constant.PLATFORM_PAYPAL);
			request.setTransactionId(transactionId);
			Map<String,String>configMap=
					cacheManage.getPaltSysConfig(CacheConstants.SYSCONFIG_KEY_PAYPAL);
			request.setConfigMap(configMap);
			//返回结果
			GetTransDetailsResponse response = paypalService.getTransactionDetails(request);
			
			if (null != response && Constant.ACK_SUCCESS.equals(response.getAck())) {
				
				GetPaypalTransactionDetailsResponse paypalResponse = response.getResponse();
				paypalResponse.setAck(Constant.ACK_SUCCESS);
				
				JSONObject paypalJson = JSONObject.fromObject(paypalResponse);
				jsonBuffer.append(paypalJson.toString());
				
			}else{
				GetPaypalTransactionDetailsResponse paypalResponse = new GetPaypalTransactionDetailsResponse();
				paypalResponse.setAck(Constant.ACK_FAIL);
				
				JSONObject paypalJson = JSONObject.fromObject(paypalResponse);
				jsonBuffer.append(paypalJson.toString());
			}
		}catch(Exception e){
			
		}
		return jsonBuffer.toString();
	}
}

